package com.example.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.core.dao.BaseCoreDao;
import com.example.core.po.BaseCorePo;
import com.example.core.po.BasePo;
import com.example.core.utils.StringUtils;
import com.example.core.vo.BaseVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class BaseCoreServiceImpl<M extends BaseCoreDao<T>, T extends BaseCorePo> extends ServiceImpl<M, T> implements BaseCoreService<T> {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String MODEL_KEY = "modelKey";

    @Override
    public void save(BasePo po) throws Exception {

    }

    @Override
    public void update(BasePo po) throws Exception {

    }

    @Override
    public void delete(BasePo po) throws Exception {
        List<String> idList = findIdList(po.getId(), po.getName(), null);
        if (!idList.isEmpty()) {
            removeByIds(idList);
        }
    }

    @Override
    public void toPo(BasePo po, BaseVo vo) throws Exception {

    }

    @Override
    public void toVo(BasePo po, BaseVo vo) throws Exception {

    }

    @Override
    public List<T> findList(String modelId, String modelName, String modelKey) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(modelId)) {
            queryWrapper.eq("model_id", modelId);
        }
        if (StringUtils.isNotEmpty(modelName)) {
            queryWrapper.eq("model_name", modelName);
        }
        if (StringUtils.isNotEmpty(modelKey)) {
            queryWrapper.eq("model_key", modelKey);
        }
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<String> findIdList(String modelId, String modelName, String modelKey) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (StringUtils.isNotEmpty(modelId)) {
            queryWrapper.eq("model_id", modelId);
        }
        if (StringUtils.isNotEmpty(modelName)) {
            queryWrapper.eq("model_name", modelName);
        }
        if (StringUtils.isNotEmpty(modelKey)) {
            queryWrapper.eq("model_key", modelKey);
        }
        return listObjs(queryWrapper, Object::toString);
    }
}
